home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 359_11 / patch5.000 / GO32_DALLOC.C < prev    next >
C/C++ Source or Header  |  1991-09-11  |  3KB  |  155 lines

  1. /* This is file DALLOC.C */
  2. /*
  3. ** Copyright (C) 1991 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. /* History:22,22 */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <dos.h>
  20. #include <io.h>
  21. #include <fcntl.h>
  22. #include <sys/stat.h>
  23.  
  24. #include "build.h"
  25. #include "types.h"
  26. #include "valloc.h"
  27. #include "dalloc.h"
  28. #include "mono.h"
  29.  
  30. #define DA_FREE    0
  31. #define DA_USED    1
  32.  
  33. #define MAX_DBLOCK 32760 /* 4095 * 8 -> 128 Mb */
  34.  
  35. static int dalloc_initted = 0;
  36. static word8 map[4096];
  37. static first_avail, last_avail;
  38. static int dfile = -1;
  39. static disk_used = 0;
  40.  
  41. extern int debug_mode;
  42.  
  43. static void dset(unsigned i, int b)
  44. {
  45.   unsigned o, m;
  46.   o = i>>3;
  47.   m = 1<<(i&7);
  48.   if (b)
  49.     map[o] |= m;
  50.   else
  51.     map[o] &= ~m;
  52. }
  53.  
  54. static int dtest(unsigned i)
  55. {
  56.   unsigned o, m;
  57.   o = i>>3;
  58.   m = 1<<(i&7);
  59.   return map[o] & m;
  60. }
  61.  
  62. static char dfilename[80];
  63.  
  64. dalloc_init()
  65. {
  66.   int i;
  67.   char *tmp;
  68.   tmp = getenv("GO32TMP");
  69.   if (!tmp) tmp = getenv("GCCTMP");
  70.   if (!tmp) tmp = getenv("TMP");
  71.   if (!tmp) tmp = getenv("TEMP");
  72.   if (!tmp) tmp = "/";
  73.   if ((tmp[strlen(tmp)-1] == '/') || (tmp[strlen(tmp)-1] == '\\'))
  74.     sprintf(dfilename, "%spage%04x.386", tmp, _CS);
  75.   else
  76.     sprintf(dfilename, "%s/page%04x.386", tmp, _CS);
  77.   dfile = open(dfilename, O_RDWR|O_BINARY|O_CREAT|O_TRUNC, S_IWRITE|S_IREAD);
  78.   if (dfile < 0)
  79.   {
  80.     printf("Fatal! cannot open swap file %s\n", dfilename);
  81.     exit(1);
  82.   }
  83.   for (i=0; i<4096; i++)
  84.     map[i] = 0;
  85.   first_avail = last_avail = 0;
  86.   dalloc_initted = 1;
  87. }
  88.  
  89. dalloc_uninit()
  90. {
  91.   if (dfile == -1)
  92.     return;
  93.   close(dfile);
  94.   unlink(dfilename);
  95. }
  96.  
  97. unsigned dalloc()
  98. {
  99.   char buf[8];
  100.   int i;
  101.   unsigned pn;
  102.   if (!dalloc_initted)
  103.     dalloc_init();
  104.   for (pn=first_avail; pn<=MAX_DBLOCK; pn++)
  105.     if (dtest(pn) == DA_FREE)
  106.     {
  107.       dset(pn, DA_USED);
  108.       first_avail = pn+1;
  109. #if TOPLINEINFO
  110.       if (pn >= last_avail)
  111.         last_avail = pn+1;
  112.       disk_used++;
  113.       sprintf(buf, "%5dk", disk_used*4);
  114.       for (i=0; i<6; i++)
  115.         poke(screen_seg, (54+i)*2, buf[i]|0x0c00);
  116. #endif
  117.       return pn;
  118.     }
  119.   printf("Fatal: out of swap space!\n");
  120.   return 0;
  121. }
  122.  
  123. void dfree(unsigned pn)
  124. {
  125.   char buf[8];
  126.   int i;
  127.   dset(pn, DA_FREE);
  128.   if (pn < first_avail)
  129.     first_avail = pn;
  130. #if TOPLINEINFO
  131.   disk_used--;
  132.   sprintf(buf, "%5dk", disk_used*4);
  133.   for (i=0; i<6; i++)
  134.     poke(screen_seg, (54+i)*2, buf[i]|0x0c00);
  135. #endif
  136. }
  137.  
  138. dwrite(word8 *buf, unsigned block)
  139. {
  140.   int c;
  141.   lseek(dfile, (long)block*4096L, 0);
  142.   c = write(dfile, buf, 4096);
  143.   if (c < 4096)
  144.   {
  145.     printf("Fatal! disk full writing to swap file\n");
  146.     exit(1);
  147.   }
  148. }
  149.  
  150. dread(word8 *buf, unsigned block)
  151. {
  152.   lseek(dfile, (long)block*4096L, 0);
  153.   read(dfile, buf, 4096);
  154. }
  155.